<template>
  <div class="publish">
    <div class="title first-title">
      <p>标题</p>
      <input
        type="text"
        placeholder="请输入广告标题(最多12字符)"
        v-model="task.task_title"
      >
    </div>

    <div class="content">
      <textarea
        placeholder="请输入广告内容...(最多150字)"
        v-model="task.task_content"
      ></textarea>
      <div class="add">
        <div class="imgbox">
        </div>
        <div
          class="uploader"
          v-show="isup"
        >
          <van-uploader :after-read="onRead">
            <img
              src="../../assets/images/photo.png"
              alt=""
            >
            <span>添加图片</span>
          </van-uploader>
        </div>
        <div class="exp">
          <p @click="showexp">查看示例</p>
        </div>
      </div>
      <div class="tips">
        *温度提示：图片比例最好为4:3，5:3,16:9
      </div>
    </div>
    <div class="tips danger">
      *温度提示：图片比例最好为4:3，5:3,16:9
    </div>
    <div class="title">
      <p>单笔广告费</p>
      <input
        type="text"
        placeholder="最低单笔广告费2元"
        v-model="task.unitprice"
      >
    </div>
    <div class="title last-title">
      <p>设置任务发布数量</p>
      <input
        type="text"
        placeholder="最低发布数量为50条"
        v-model="task.task_num"
      >
    </div>
    <div class="title last-title">
      <p>广告总价</p>
      <p>{{parseInt(task.task_num) * parseInt(task.unitprice) ? parseInt(task.task_num) * parseInt(task.unitprice) : 0}}</p>
    </div>
    <div class="lizi" @click="hide">
      <img
        src="../../assets/images/pengyouquan.jpg"
        alt=""
      >
    </div>

    <div class="submit">
      <button @click="submit">确认发布</button>
    </div>

  </div>
</template>
<script>
import { Icon } from "vant";
import { Toast } from "vant";
import { fileUploadOne, publishTask } from "../../api/user";
export default {
  data() {
    return {
      task: {
        imageurl_arr: [],
        task_title: "",
        task_content: "",
        unitprice: "",
        task_num: null,
        totalprice: ""
      }
    };
  },
  computed: {
    isup() {
      let is = true;
      console.log(this.task.imageurl_arr);
      if (this.task.imageurl_arr.length >= 3) {
        Toast("最多只可上传3张");
        is = false;
      }
      return is;
    }
  },
  methods: {
    showexp() {
      let lizi = document.getElementsByClassName("lizi");
      console.log(lizi);
      lizi[0].style.transform = "scale(1)";
    },
    hide() {
      let lizi = document.getElementsByClassName("lizi");

      lizi[0].style.transform = "scale(0)";
    },
    onRead(file) {
      let fd = new FormData();
      fd.append("image", file.file);
      fileUploadOne(fd).then(res => {
        if (res.status == 200) {
          this.task.imageurl_arr.push(res.data.imageurl);
          const img = document.createElement("img");
          img.src = res.data.imageurl;
          const imageBox = document.querySelector(".imgbox");
          imageBox.appendChild(img);
        }
      });
    },
    submit() {
      if (!this.task.task_title) {
        Toast("请输入标题");
        return;
      }
      if (!this.task.task_content) {
        Toast("请输入广告内容");
        return;
      }
      if (!this.task.unitprice) {
        Toast("请输入单笔广告费");
        return;
      }
      if (!this.task.task_num) {
        Toast("请输入发布数量");
        return;
      }
      if (parseInt(this.task.task_num) < 50) {
        Toast("发布数量不可低于50条");
        return;
      }
      this.task.totalprice =
        parseInt(this.task.task_num) * parseInt(this.task.unitprice)
          ? parseInt(this.task.task_num) * parseInt(this.task.unitprice)
          : 0;
      publishTask(this.task).then(res => {
        if (res.status == 200) {
          Toast(res.message);
        }
      });
    }
  }
};
</script>
<style lang="scss">
.publish {
  width: 100%;
  background-color: #fff;
  header {
    color: #fff;
  }

  .title {
    display: flex;
    display: -webkit-flex;
    justify-content: space-between;
    padding: 20px 5%;
    border-top: 1px solid #dcdcdc;
    font-size: 12px;
    align-items: center;
    p {
      font-size: 16px;
      font-weight: 500;
    }
    input {
      font-size: 12px;
      border: none;
      outline: none;
      width: 50%;
      text-align: right;
    }
  }
  .first-title {
    justify-content: flex-start;
    border: none;
    input {
      border-radius: 4px;
      margin-left: 20px;
      width: 80%;
      height: 35px;
      border: none;
      outline: none;
      border: 1px solid #dcdcdc;
      padding-left: 10px;
      text-align: left;
    }
  }
  .last-title {
    border-bottom: 1px solid #eee;
  }
  .lizi {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    transition: 0.5s all;
    transform: scale(0);
    img {
      width: 100%;
    }
  }
  .content {
    .exp {
      position: relative;
      text-decoration: blue;
      p {
        color: #F11633;
        font-size: 16px;
        text-decoration: underline;
        white-space: nowrap;
        position: absolute;
        left: 2px;
        bottom: 2px;
      }
    }
    width: 100%;

    border: 1px solid #dcdcdc;
    textarea {
      padding: 10px 0 0 10px;
      width: 100%;
      height: 80%;
      outline: none;
      border: none;
    }
    textarea::-webkit-input-placeholder {
      /* placeholder颜色  */
      color: #999;
      /* placeholder字体大小  */
      font-size: 16px;
      /* placeholder位置  */
      // text-align: right;
    }
    .add {
      width: 100%;
      display: flex;
      display: -webkit-flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      .uploader {
        margin-left: 10px;
        width: 100px;
        height: 100px;
        border: 1px dashed #eee;
        text-align: center;
        border-radius: 5px;
        .van-uploader {
          width: 100%;
          height: 100%;
          display: flex;
          display: -webkit-flex;
          justify-content: center;
          align-items: center;
          flex-direction: column;
        }
        img {
          width: 25px;
          margin-bottom: 5px;
        }
        span {
          // font-size: 100px;
          // font-weight: 100;
          color: #999;
          display: inline-block;
          height: 20px;
        }
      }
      .imgbox {
        img {
          width: 100px;
          height: 100px;
          margin-left: 10px;
        }
      }
    }
  }
  .tips {
    padding: 10px;
    color: #999;
    font-size: 12px;
  }
  .danger {
    color: red;
  }
  .submit {
    width: 90%;
    margin: 20px auto;

    button {
      border: none;
      outline: none;
      width: 100%;
      height: 40px;
      color: #fff;
      font-size: 16px;
      letter-spacing: 2px;
      background-color: #e71434;
      border-radius: 5px;
    }
  }
}
</style>


